/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.drug.dao.DrugStorageProfileDao;
import com.jims.master.modules.drug.entity.DrugStorageProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 药品库存上下限Service
 * @author DT
 * @version 2017-03-28
 */
@Service
@Transactional(readOnly = true)
public class DrugStorageProfileService extends CrudService<DrugStorageProfileDao, DrugStorageProfile> {
	@Autowired
	private DrugStorageProfileDao drugStorageProfileDao;
	public DrugStorageProfile get(String id) {
		return super.get(id);
	}
	
	public List<DrugStorageProfile> findList(DrugStorageProfile drugStorageProfile) {
		return super.findList(drugStorageProfile);
	}
	
	public Page<DrugStorageProfile> findPage(Page<DrugStorageProfile> page, DrugStorageProfile drugStorageProfile) {
		return super.findPage(page, drugStorageProfile);
	}

	public Page<DrugStorageProfile> findStorageWarning(Page<DrugStorageProfile> page, DrugStorageProfile drugStorageProfile) {
		drugStorageProfile.setPage(page);
		page.setList(drugStorageProfileDao.findStorageWarning(drugStorageProfile));
		return page;
	}

    /*
    * 获取一段时间内药品消耗量的list
    * ZhuQ
    * */
    public Page<DrugStorageProfile> findMinMaxList(Page<DrugStorageProfile> page, DrugStorageProfile drugStorageProfile) {
        drugStorageProfile.setPage(page);
        page.setList(drugStorageProfileDao.findMinMaxList(drugStorageProfile));
        return page;
    }

    /*
    * 根据infoId获取一条消耗量数据
    * ZhuQ
    * */
    public DrugStorageProfile findByInfoId(DrugStorageProfile drugStorageProfile) {
        List<DrugStorageProfile> list=drugStorageProfileDao.findMinMaxList(drugStorageProfile);
        drugStorageProfile=list.get(0);
		if(StringUtils.isNotBlank(drugStorageProfile.getQuantity())){
			drugStorageProfile.setQuantity(String.valueOf(Double.valueOf(drugStorageProfile.getQuantity())/Double.valueOf(drugStorageProfile.getPackageNum())));
		}
        if(!"".equals(drugStorageProfile.getOutQuantity() )&& null !=drugStorageProfile.getOutQuantity()){
            drugStorageProfile.setOutQuantity(String.valueOf(Double.valueOf(drugStorageProfile.getOutQuantity()) / Double.valueOf(drugStorageProfile.getPackageNum())));
        }
        if(!"".equals(drugStorageProfile.getMinQuantity() )&& null !=drugStorageProfile.getMinQuantity()){
            drugStorageProfile.setMinQuantity(String.valueOf(Double.valueOf(drugStorageProfile.getMinQuantity()) / Double.valueOf(drugStorageProfile.getPackageNum())));
        }
        if(!"".equals(drugStorageProfile.getMaxQuantity() )&& null !=drugStorageProfile.getMaxQuantity()){
            drugStorageProfile.setMaxQuantity(String.valueOf(Double.valueOf(drugStorageProfile.getMaxQuantity()) / Double.valueOf(drugStorageProfile.getPackageNum())));
        }
        return drugStorageProfile;
    }
	
	@Transactional(readOnly = false)
	public int save(DrugStorageProfile drugStorageProfile) {
		return super.save(drugStorageProfile);
	}
	
	@Transactional(readOnly = false)
	public int delete(DrugStorageProfile drugStorageProfile) {
		return super.delete(drugStorageProfile);
	}
	
}